package com.campus.web.control;

import com.campus.model.Course;
import com.campus.model.Teacher;
import com.campus.service.CourseService;
import com.campus.service.TeacherService;
import com.campus.web.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @desc: 教师控制器类
 * @author: HLQ
 * @date: 2024/11/23 1:30
 */
@Slf4j
@RestController
@RequestMapping("/teachers")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;


    @PostMapping()
    public ResponseResult<String> addTeacher(@RequestBody @Validated Teacher teacher) {
        log.info("Adding teacher: {}", teacher);
        int result = teacherService.addTeacher(teacher);
        if (result > 0) {
            return ResponseResult.ok("教师添加成功");
        } else {
            return ResponseResult.error(HttpStatus.BAD_REQUEST,"添加失败");
        }
    }


    @GetMapping("/search")
    public ResponseResult<Teacher> queryTeacherById(@RequestParam @NotBlank int id) {
        log.info("Querying teacher with ID: {}", id);
        Teacher teacher = teacherService.queryTeacherById(id);
        if (teacher != null) {
            return ResponseResult.ok(teacher, "查询成功");
        } else {
            return ResponseResult.error(HttpStatus.BAD_REQUEST,"未找到教师信息");
        }
    }
    @PostMapping("/courses")
    public ResponseResult<String> addTeacherCourse(@RequestBody @Validated Course course) {
        log.info("Adding course: {}", course);
        int result = courseService.addCourse(course);
        if (result > 0) {
            return ResponseResult.ok("课程添加成功");
        } else {
            return ResponseResult.error(HttpStatus.BAD_REQUEST,"添加失败");
        }
    }

}
